fn main() {
    let number_list = vec![1, 123, 33, 44,0, 12];
    println!("{}", largest(&number_list));

    let integer = Point {x: 10, y: 20};
    let float = Point {x: 1.1f32, y: 2.0f32};
    println!("{} {}", integer.x(), float.x());
    println!("{}", float.distance());
    // println!("{}", integer.distance()); // method not found in `Point<{integer}>
}

// 函数定义泛型
fn largest<T: PartialOrd>(list: &[T]) -> &T {
    let mut largest = &list[0];
    for x in list {
        if x > largest {
            largest = x;
        }
    }

    largest
}

// 结构体泛型
struct Point<T> {
    x: T,
    y: T,
}

// 方法泛型
impl<T> Point<T> {
    fn x(&self) -> &T {
        &self.x
    }
}

// 某个具体类型的泛型
impl Point<f32> {
    fn distance(&self) -> f32 {
        (self.y.powi(2) + self.x.powi(2)).sqrt()
    }
}
